import React from 'react'
import { View, Image } from 'react-native'
import { Lt, Color, Jpp, Jx, Flist, Dialog } from "../_utils"
import { cHome } from "../home/_home"
import { FlistHeadLine, FlistGoodsEmpty } from '../_util';
import { TouchGoodsVer } from "./ApplyGoods"


export class GoodsList extends React.PureComponent{
    constructor(props){
        super(props);
        const { appIdx } = this.props;
        this.appIdx = appIdx;
        this.onItem = this.onItem.bind(this);
    }
    onItem(params){
        const idx = params.index;
        const item = params.item;
        const wh = Jpp.width / 2 - Lt.margin - 5 - 3; // 框框线本身也算
        const style = {
            marginLeft : (idx%2)?1:5,
            marginRight : (idx%2)?5:1,
            marginTop:Lt.margin,
        }
        let newData = cHome.toData(item, this.appIdx);
        if(!newData.imgUrl){
            const details = cHome.getImages(newData.goodId);
            if(details){
                newData.imgUrl = details[0];
                newData.details = details;
            }
        }
        return <TouchGoodsVer
            wh = {wh}
            style={style}
            data = {newData}
        />
    }
    refresh(){
        if(this.flist){
            // if(this.appIdx == eApp.rdxs[eApp.PDD]){
            //     this.flist.clearStateArray();
            // }
            this.flist.refresh();
            this.flist.toTop();
        }
    }
    setAppIdx(appIdx){
        this.appIdx = appIdx;
    }
    render(){
        const {onLoad, ...other} = this.props;
        return <Flist style = {{flex:1}}
            numColumns = {2}
            onLoad = {onLoad || null}
            onItem = {this.onItem}
            ListEmptyComponent = {<FlistGoodsEmpty/>}
            ListHeaderComponent = {<FlistHeadLine/>}
            stickyHeaderIndices = {[0]}
            ref={r=>this.flist=r}
            {...other}
        />
    }
}